<?php 

class Upload{

	private $config = array(

				'maxSiez' =>  0,            //上传文件最大大小 0 不限大小
				'exts'   => array(),        //允许上传文件后缀
				'rootPath' => './Uploads'  //上传文件根目录
		);

	private $error = '';     //保存错误信息

	public function __construct($config = array()){
		//初始化配置项
		$this->config = array_merge($this->config,$config);
		
		if (!empty($this->config['exts'])) {
			//如果是字符串类型则转换成数组
			if (is_string($this->config['exts'])) {
				$this->config['exts'] = explode(',',$this->config['exts']);
			}
			//将字符全部小写
			$this->config['exts'] = array_map('strtolower',$this->config['exts']);
		}

	}

	public function upload () {
		$files = $_FILES;
		//检查文件夹
		if (!$this->checkRootPath()) {
			$this->error='您上传目录'.$this->config['rootPath'].'不存在，请尝试手动创建';
			return false;
		}
		if (empty($files)) {
			$this->error = '没有上传文件';
			return false;
		}
		
	}

	/**
	 * [checkRootPath 检查文件夹是否存在]
	 * @return [bool] [description]
	 */
	private function checkRootPath () {
		if (!file_exists($this->config['rootPath'])) {
			return $this->mkdir($this->config['rootPath']));
		}
		return true;
	}
	/**
	 * [mkdir 创建目录]
	 * @param  [string] $file [要创建的文件夹]
	 * @return [bool]       [description]
	 */
	private function mkdir ($file) {
		return mkdir($file,0777,true);
	}
}